package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.RoleBizService;
import com.neusoft.weekreport.component.entities.BgRole;
import com.neusoft.weekreport.component.entities.BgRoleExample;
import com.neusoft.weekreport.component.entities.BgRoleRefMenuExample;
import com.neusoft.weekreport.component.entities.BgUserRefRoleExample;
import com.neusoft.weekreport.component.req.GetAddRoleInfoReq;
import com.neusoft.weekreport.component.req.GetRoleInfoByIdReq;
import com.neusoft.weekreport.component.resp.GetAddRoleInfoResp;
import com.neusoft.weekreport.component.resp.GetRoleInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetRoleInfoListResp;
import com.neusoft.weekreport.mapper.BgRoleMapper;
import com.neusoft.weekreport.mapper.BgRoleRefMenuMapper;
import com.neusoft.weekreport.mapper.BgUserRefRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RoleBizServiceImpl implements RoleBizService {

    @Resource
    BgRoleMapper bgRoleMapper;
    @Resource
    BgUserRefRoleMapper bgUserRefRoleMapper;
    @Resource
    BgRoleRefMenuMapper bgRoleRefMenuMapper;

    //查询角色树
    @Override
    public GetRoleInfoListResp getRoleInfoList() throws Exception {
        GetRoleInfoListResp resp = new GetRoleInfoListResp();
        BgRoleExample bgRoleExample = new BgRoleExample();
        BgRoleExample.Criteria criteria = bgRoleExample.createCriteria();
        criteria.andEnableEqualTo(1);
        List<BgRole> bgRole = bgRoleMapper.selectByExample(bgRoleExample);
        resp.setRoleInfos(bgRole);
        return resp;
    }

    //通过id查询角色信息
    @Override
    public GetRoleInfoByIdResp getRoleInfoById(GetRoleInfoByIdReq req) throws Exception {
        GetRoleInfoByIdResp resp = new GetRoleInfoByIdResp();
        BgRole bgRole = bgRoleMapper.selectByPrimaryKey(req.getId());
        resp.setId(req.getId());
        resp.setRoleName(bgRole.getRoleName());
        resp.setDescription(bgRole.getDescription());
        if(bgRole.getParentId().equals("-1")){
            resp.setParentName("无");
        }else{
            resp.setParentId(bgRole.getParentId());
            BgRole bgRole1 = bgRoleMapper.selectByPrimaryKey(bgRole.getParentId());
            resp.setParentName(bgRole1.getRoleName());
        }
        return resp;
    }

    //添加角色
    @Override
    public GetAddRoleInfoResp getAddRoleInfo(GetAddRoleInfoReq req) throws Exception {
        String id = UUID.randomUUID().toString();
        GetAddRoleInfoResp resp =new GetAddRoleInfoResp();
        resp.setId(id);
        String NowDate = sysDateTime();
        BgRole bgRole = new BgRole();
        bgRole.setId(id);
        bgRole.setRoleName(req.getRoleName());
        bgRole.setDescription(req.getDescription());
        bgRole.setParentId(req.getParentId());
        bgRole.setEnable(1);
        bgRole.setDeleteable(1);
        bgRole.setCreateDate(NowDate);
        bgRole.setUpdateDate(NowDate);
        bgRoleMapper.insert(bgRole);
        return resp;
    }

    //修改角色
    @Override
    public void getEditRoleInfo(GetAddRoleInfoReq req) throws Exception {
        String NowDate = sysDateTime();
        BgRole bgRole = new BgRole();
        bgRole.setId(req.getId());
        bgRole.setRoleName(req.getRoleName());
        bgRole.setParentId(req.getParentId());
        bgRole.setDescription(req.getDescription());
        bgRole.setUpdateDate(NowDate);
        bgRoleMapper.updateByPrimaryKeySelective(bgRole);
    }

    //删除角色
    @Override
    @Transactional
    public void getDeleteRoleInfo(GetAddRoleInfoReq req) throws Exception {

        //删除角色
        bgRoleMapper.deleteByPrimaryKey(req.getId());

        //删除用户和角色关联表
        BgUserRefRoleExample bgUserRefRoleExample = new BgUserRefRoleExample();
        BgUserRefRoleExample.Criteria criteria = bgUserRefRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(req.getId());
        bgUserRefRoleMapper.deleteByExample(bgUserRefRoleExample);

        //删除角色和菜单关联表
        BgRoleRefMenuExample bgRoleRefMenuExample = new BgRoleRefMenuExample();
        BgRoleRefMenuExample.Criteria criteria1 = bgRoleRefMenuExample.createCriteria();
        criteria1.andRoleIdEqualTo(req.getId());
        bgRoleRefMenuMapper.deleteByExample(bgRoleRefMenuExample);

    }

    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }
}
